package com.lightsh.things;

import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.World;
import org.jbox2d.dynamics.contacts.ContactPoint;
import android.graphics.RectF;
import com.lightsh.body.CircleImpl;
import com.lightsh.gui.PhysicsScreen;

/**
 * 子弹类, 该类使用的单位为px
 * @author Light
 *
 */
public class BulletCircle extends CircleImpl{

	private boolean mbNeedDestroy = false;
	/**
	 * 注意: 该类使用的单位为px
	 */
	public BulletCircle(PhysicsScreen screen, float x, float y, float radius,
			float density, float friction, float restitution) {
		super(screen, x, y, radius, density, friction, restitution, true);
	}
	@Override
	public void update() {
		super.update();
		if(mbNeedDestroy){
			mBody.m_world.destroyBody(mBody);
			mbNeedDestroy = false;
		}else{
			// 当离开屏幕时自动销毁
			Vec2 vec2 = getPosition();
			if(!mParentScreen.isInWorld(new RectF(vec2.x - mCircleDef.radius, vec2.y - mCircleDef.radius, 
					vec2.x + mCircleDef.radius, vec2.y + mCircleDef.radius))){
				mBody.m_world.destroyBody(mBody);
			}
		}
	}
	@Override
	public void onContactAdd(ContactPoint point) {
		super.onContactAdd(point);
		// 需要处理的碰撞信息
		if(point.shape1.m_density == 0 || point.shape2.m_density == 0){
//			if(point.velocity.length() > 30){
//				mbNeedDestroy = true;
//			}
//			mbNeedDestroy = true;
		}
	}
	public void applyForce(Vec2 v){
		mBody.applyForce(v, mBody.getWorldCenter());
	}
	
}
